package com.example.ware.aop;


import com.example.ware.exceptions.TokenException;
import com.example.ware.utils.Ujwt;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Aspect // 声明为AOP切面
@Component // 注册为Spring组件，以便进行AOP拦截
public class TokenAspect {


    @Resource
    Ujwt ujwt;

    @Resource
    RedisTemplate redisTemplate;

    @Resource
    private HttpServletRequest request;

    @Before("@annotation(com.example.ware.annotation.Token)")
    public void validateToken(JoinPoint joinPoint) {

                String token = request.getHeader("token"); // 获取请求头中名为 "token" 的参数
                if(token==null){
                    throw new TokenException();
                }
                if (redisTemplate.opsForValue().get("Admin_"+token)==null){
                    throw new TokenException();
                }

    }

}
